<?php
/**
 * 全局控制
 *
 * @copyright  Copyright (c) 2015-2025 NcPHP Inc. (http://www.ncphp.com)
 * @license    http://www.ncphp.com/
 * @link       http://www.ncphp.com/
 * @author     NcPHP Team
 * @since      File available since Release v1.1
 */
defined('InNcPHP') or exit('Access Invalid!');

# 总控制器
class Control{
	
    public function __construct(){
        global $NC; // 全局常用参数
        $phone = isset($_GET['phone'])?$_GET['phone']:$_POST['phone'];
        $sign = isset($_GET['sign'])?$_GET['sign']:$_POST['sign'];
        $this->_check_sign($phone,$sign);
//        header('Content-type: application/json');
    }

    /**
     *  2000    获取成功
     *  4000    错误请求(参数验证错误)
     *  4001    请求要求身份验证(需要登录\token过期)
     *  4003    服务器拒绝请求(提交参数有误)
     *  4004    找不到数据
     *  4009    冲突(已经存在)
     *  4010    已经删除
     *  5000    服务器内部错误
     */
    //将json数据输出
    public function response($data,$code=2000,$message='获取成功',$flag=true){
        header('Content-type:text/json');
        $param = array();
        $param['code'] = $code;
        if($flag){
            switch ($code){
                case 4001:
                    $message = '签名错误';
                    break;
                case 5000:
                    $message = '服务器内部错误';
                    break;
            }
        }
        $param['message'] = $message;
        $param['data'] = $data==''?(object)array():$data;
        die(json_encode($param));
    }

    /**
     * 需要分页时,返回数据封装
     * @param $list
     * @param $count
     * @param $page_size
     * @param $page
     * @return array
     */
    public function responsePage($list,$count, $page, $page_size){
        header('Content-type:text/json');
        $total_count=$count;
        $total_page=floor($count / $page_size);
        if($count % $page_size>0) $total_page++;
        if($page > $total_page){
            $list = array();
        }
        $data=array('data'=>$list,'total_page'=>$total_page,'count'=>$total_count, 'page'=>$page);
        return $data;
    }

    /*
     * 验证token,通过返回用户member_id
     */
    public function checkToken($token){
        if(empty($token)){
            $this->response('',4003,'请传token参数');
        }
        $member = Model('member');
        $memberInfo = $member->getMemberInfo(array('token'=>$token));
        if(empty($memberInfo)){
            $this->response('',4001);
        }else{
            if(time()-$memberInfo['login_time'] > 86400*7){
                $this->response('',4001);
            }else{
                return $memberInfo['member_id'];
            }
        }
    }



    //判断是否post方式提交
    public function isPost(){
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            return true;
        }else{
            $this->response('',4003,'请用post方式请求');
        }
    }

    //判断是否get方式提交
    public function isGet(){
        if($_SERVER['REQUEST_METHOD'] == 'GET'){
            return true;
        }else{
            $this->response('',4003,'请用GET方式请求');
        }
    }

    /**
     * 验证签名
     * @mobile  手机号
     * @sian 签名
     */
    private function _check_sign($moblie,$sian)
    {
        $_sign = md5($moblie.API_KEY);
        if($_sign==$sian){
            return true;
        }else{
            $this->response(array(),'4001','签名错误');
        }
    }


    /**
     * post 提交数据
     * @param $url
     * @param $data
     * @param array $options
     * @return mixed
     */
    public function httpPost($url, $data, $options = array())
    {
        $ch = curl_init($url);
        curl_setopt_array($ch, $options + array(
                CURLOPT_POST => true,
                CURLOPT_HEADER => false,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_POSTFIELDS => $data,
                CURLOPT_SSL_VERIFYPEER => false,
            ));
        $res = curl_exec($ch);
        curl_close($ch);
        return $res;
    }

    /**
     * HTTP/HTTPS请求
     * @param string $url 请求的URL
     * @param array $options CURL选项
     * @return mixed 成功返回获取到的数据,失败返回false
     */
    public function httpGet($url, $options = array())
    {
        $ch = curl_init($url);
        curl_setopt_array($ch, $options + array(
                CURLOPT_HEADER => false,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_SSL_VERIFYPEER => false,
            ));
        $res = curl_exec($ch);
        curl_close($ch);
        return $res;
    }

    /**
     * 接口输出
     */
    protected function msgbox($msg, $data=array(), $code = 0)
    {
        $data = array('code' => $code, 'msg' => $msg, 'data' => $data == '' ? new stdClass() : $data);
        if (!empty($data)) {
            if (!is_array($data)) {
                exit($data);
            } else {
                $str = json_encode($data);
                $str = str_replace('null', '""', $str);
                header('Content-Type: application/json;charset=' . 'UTF-8');
                echo $str;
                exit();
            }
        }
        //exit;
    }

}
?>
